import Vue from 'vue'
import Router from 'vue-router'
import {map} from './map'
import {getLoginStatus} from '../libs/common'

Vue.use(Router)

export const router = new Router({routes: map})

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 路由是否需要用户登陆
    if (!getLoginStatus()) {
      // 未登录
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    } else {
      // 登陆后超时
      next()
    }
  } else {
    next()
  }
})
